﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulldogCMS.Core.Storage;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using BulldogCMS.Core.Storage.DataTypes;
using BulldogCMS.Core.Storage.Cache;
using BulldogCMS.Core.Storage.Providers;
using BulldogCMS.Core.Runtime.Modules;
using BulldogCMS.Core.MVC;
using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing;

namespace BulldogCMS.Core.Runtime
{
    public class Runtime
    {
        public static void LoadApplicationContext()
        {
            Trace.TraceInformation("Bulldog Context Load Start", "BulldogCMS Internal");
            
            XDocument configurationDoc =                    LoadConfigurationFile(HttpContext.Current.Server.MapPath("/"));
            XDocument treeDoc =                             LoadTreeFile(HttpContext.Current.Server.MapPath("/"));
            Context.InitApplicationContext(configurationDoc, treeDoc, HttpContext.Current.Server.MapPath("/"));
            Trace.TraceInformation("Bulldog Context Load End", "BulldogCMS Internal");
        }

        public static void SetControllerFactory()
        {
            ControllerBuilder.Current.SetControllerFactory(new BulldogCMS.Core.MVC.ControllerFactory());
        }

        public static void SetVirtualProvider()
        {
            System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new VirtualFileProvider());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            // module paths loader
            foreach (var areaProvider in Context.Application.AreaProviders)
            {
                routes.MapRoute(
                    areaProvider.AreaName + "_route",
                    areaProvider.AreaName + "/{controller}/{action}/{id}/{parm1}/{parm2}",
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional, parm1 = UrlParameter.Optional, parm2 = UrlParameter.Optional } // Parameter defaults
                );
            }
            
            routes.MapRoute(
                "withArea", // Route name
                "withArea/{area}/{controller}/{action}/{id}/{parm1}/{parm2}", // URL with parameters
                new { area = "default", controller = "Home", action = "Index", id = UrlParameter.Optional, parm1 = UrlParameter.Optional, parm2 = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "withoutArea", // Route name
                "withoutArea/{controller}/{action}/{id}/{parm1}/{parm2}", // URL with parameters
                new { area = "default", controller = "Home", action = "Index", id = UrlParameter.Optional, parm1 = UrlParameter.Optional, parm2 = UrlParameter.Optional } // Parameter defaults
            );
        }

        private static XDocument LoadConfigurationFile(string applicationPhysicalPath)
        {
            string configurationFilePath = Path.Combine(applicationPhysicalPath, "Configuration.xml");
            return XDocument.Load(configurationFilePath);
        }

        private static XDocument LoadTreeFile(string applicationPhysicalPath)
        {
            string treeFilePath = Path.Combine(applicationPhysicalPath, "Tree.xml");
            return XDocument.Load(treeFilePath);
        }


    }
}